//
//  WPWelcomeView.swift
//  WB
//
//  Created by 汪鹏 on 2017/1/4.
//  Copyright © 2017年 汪鹏. All rights reserved.
//

import UIKit
import SDWebImage

class WPWelcomeView: UIView {
    
    // 用户头像
    @IBOutlet weak var userIconView: UIImageView!
    
    // 显示“欢迎回来”的label
    @IBOutlet weak var welcomeBackTitle: UILabel!
    
    // 用户头像的底部约束
    @IBOutlet weak var iconBottomCons: NSLayoutConstraint!
    
    
    /*
     
     /// 该方法是二进制xib文件加载完成时调用，这时候其他控件还没被初始化出来
     required init?(coder aDecoder: NSCoder) {
        
    }
     */
    
    /// xib加载完成
    override func awakeFromNib() {
                
        guard let urlString = WPNetManager.shareManager.userAccount.avatar_large,
        let url = URL(string: urlString) else {
            
            print(#function+"----"+"设置用户头像失败")
            
            return
        }
        //(red: 246, green: 246, blue: 246, alpha: 1)
        UIImage.downLoadImage(url: url, imageView: userIconView, backColor: UIColor(red: 246, green: 246, blue: 246, alpha: 0.98))
        
    }
    
    /// 类方法快速创建视图
    ///
    /// - Returns: WPWelcomeView
    class func welcomeView() -> WPWelcomeView {
        
        let nib = UINib(nibName: "WPWelcomeView", bundle: nil)
        
        let welcome = nib.instantiate(withOwner: nil, options: nil)[0] as! WPWelcomeView
        
        // 从xib加载的视图默认大小都是600*600
        welcome.frame = UIScreen.main.bounds
        
        return welcome
        
    }
    
    /// 视图被添加到window上， 表示视图已经显示出来
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        layoutIfNeeded()
        
        iconBottomCons.constant = frame.height - 200
        
        UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {
            
            self.layoutIfNeeded() // 更新约束
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { 
                
                self.welcomeBackTitle.alpha = 1

            }, completion: { (_) in
            
                self.removeFromSuperview()
            })
            
        }
        
    }

}
